timer = {
  eventstack = list.new()
  
}

ringbuffer = {}
ringbuffer.ring = {}
ringbuffer.index = 0
ringbuffer.insert = function(self, item, compare)
  

end

ringbuffer.remove = function(self, itemorindex)

end

ringbuffer.next = function(self)
  self.index = self.index + 1
  if self.index > #self.ring then
    self.index = 0
  end
  return self.ring[self.index] 
end

ringbuffer.step = function(self, predicate)
  local next = 0
  while true do
    local = self.next()
    if predicate() then
    
    else
      return
    end
  end  
end

work = function (data)
  print(data,"lala")
  return true
end

thread = function (name, worker, data, timeslice, critical)
  local t = {}
  t.name = name
  t.worker = worker
  t.data = data or {}
  t.timeslice = timeslice
  t.critical = critical
  t.thread = coroutine.create(
  function(t)
    local ok = true
    while true do
      local complete = false
      local critical = t.critical --if critical we work till we finish,
                                --and alert if we overshoot our timeslice
                                --else we yield upon time slice is up
      local timeout = os.clock() + t.timeslice
      complete = false      
      while not(complete == true) do
        if (timeout < os.clock()) then
          if critical then
            print(t.name, "'s work might be overloaded")            
          else
            break
          end  
        end
        ok, complete = pcall(t.worker,t)
        if ok == false then
          --work screwed up so log a message
          print(t.name,"'s worker caused an error in its protected enviroment")
        end 
      end
      t = coroutine.yield(complete)
    end
  end
  )
  t.__call = function (self)
    coroutine.resume(self.thread,self)
  end
  setmetatable(t,t)
  return t
end

t = thread("test",work,"data",0.02,false)

createtask = function(name,t)
   local task = {
     complete = false,
     __name = name,
     subtasks = list.new(),
     addsubtask = function(self,subtask)
       self.subtasks:pushleft(subtask)
     end,
     removesubtask = function(self,subtask)
       self.subtasks:popleft()
     end,
     check = function(self,dt)
       if self.complete == true then
         print(self.__name.." Task Finished")
         return "finished","continue"       
       end
       if self.subtasks:length() == 0 then
         self.complete = true
         print(self.__name.." Task Block Siblings")
         return "inprogress","blocksiblings"
       else
         print(self.__name.." Task Continue")
         return "inprogress","continue"
       end
     end,
     act = function(self,dt)
       local finished, state = "finished", "inprogress"
       -- If all subtasks are finished, and we are finished finished is true
       local st
       for st in entries(self.subtasks) do
         finished,state = st:act(dt)
         if finished == "finished" then
           self:removesubtask(st)
         end
         if state == "continue" then
           -- Task is in progress and other tasks may continue
         elseif state == "blocksiblings" then
           -- Task is in progress and blocking other sibling tasks (that follow this one)
           break
         elseif state == "blockall" then
           -- Task needs full attention and is blocking all other tasks
           return finished, state
         end
       end 
       -- Do task logic
       
       -- Check if finish conditions have been met
       finished, state = self:check(dt)
       return finished, state  
     end,
  }
  return task
end

taskA = createtask("A")
taskB = createtask("B")
taskC = createtask("C")
taskD = createtask("D")

taskA:addsubtask(taskB)
taskA:addsubtask(taskC)
taskA:act() -->C Inprogress
taskA:act() -->C Finished
taskA:addsubtask(taskD)
taskA:act() -->D Inprogress
taskA:act() -->D Finished
taskA:act() -->B Inprogress
taskA:act() -->B & A Finished
taskA:act() -->A Finished
